package testing;

import static org.junit.Assert.*;

import java.util.Iterator;

import hypeerweb.Connections;
import hypeerweb.Node;
import hypeerweb.WebId;

import org.junit.Test;

public class ConnectionsTest {

	@Test
	public void testConstructor() {
		Connections conn1 = new Connections();
		assertTrue(conn1.getDownPointers().isEmpty());
		Node par = new Node(3);
		Connections conn2 = new Connections(conn1, par);
		assertTrue(conn2.getSurrogateFold() == Node.NULL_NODE);
	}

	@Test
	public void testGetLargestConnection() {
		Connections conn1 = new Connections();

		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		Node n5 = new Node(5);

		conn1.addNeighbor(n1, n1.getWebId());
		conn1.addNeighbor(n2, n1.getWebId());
		conn1.addNeighbor(n3, n1.getWebId());
		conn1.setFold(n4);
		conn1.addUpPointer(n5);

		assertTrue(conn1.getLargestConnection().compareTo(n5) == 0);
	}

	@Test
	public void testGetNeighbors() {

		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		Node n5 = new Node(5);

		Connections conn1 = new Connections(n1.getConnections(), n1);
		conn1.addNeighbor(n2, n1.getWebId());
		conn1.addNeighbor(n3, n1.getWebId());

		assertTrue(conn1.getNeighborsBig().size() == 2);
		assertTrue(conn1.getNeighbors().size() == 3);

		assertTrue(conn1.getNeighbors().contains(n1));
		assertTrue(conn1.getNeighbors().contains(n2));
		assertTrue(conn1.getNeighbors().contains(n3));

		assertTrue(conn1.getNeighborsBig().contains(n2));
		assertTrue(conn1.getNeighborsBig().contains(n3));
	}

	@Test
	public void testUpPointers() {
		Connections conn1 = new Connections();

		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		Node n5 = new Node(5);

		conn1.addUpPointer(n1);
		conn1.addUpPointer(n2);
		conn1.addUpPointer(n3);
		conn1.addUpPointer(n4);
		conn1.addUpPointer(n5);

		assertTrue(conn1.getUpPointers().size() == 5);

		conn1.removeUpPointer(n1);
		conn1.removeUpPointer(n2);

		assertTrue(conn1.getUpPointers().size() == 3);

		assertTrue(conn1.getUpPointers().contains(n3));
		assertTrue(conn1.getUpPointers().contains(n4));
		assertTrue(conn1.getUpPointers().contains(n5));

		assertFalse(conn1.getUpPointers().contains(n1));
		assertFalse(conn1.getUpPointers().contains(n2));

		conn1.deleteUpPointers();

		assertTrue(conn1.getUpPointers().isEmpty());

	}

	@Test
	public void testDownPointers() {
		Connections conn1 = new Connections();

		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		Node n5 = new Node(5);

		conn1.addDownPointer(n1);
		conn1.addDownPointer(n2);
		conn1.addDownPointer(n3);
		conn1.addDownPointer(n4);
		conn1.addDownPointer(n5);

		assertTrue(conn1.getDownPointers().size() == 5);

		conn1.removeDownPointer(n1);
		conn1.removeDownPointer(n2);

		assertTrue(conn1.getDownPointers().size() == 3);

		assertTrue(conn1.getDownPointers().contains(n3));
		assertTrue(conn1.getDownPointers().contains(n4));
		assertTrue(conn1.getDownPointers().contains(n5));

		assertFalse(conn1.getDownPointers().contains(n1));
		assertFalse(conn1.getDownPointers().contains(n2));

	}

	@Test
	public void testRemoveNeighbor() {

		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);

		Connections conn1 = new Connections(n1.getConnections(), n1);

		conn1.addNeighbor(n2, n1.getWebId());
		conn1.addNeighbor(n3, n1.getWebId());

		assertTrue(conn1.getNeighbors().size() == 3);

		conn1.removeNeighbor(n4);// should do nothing

		assertTrue(conn1.getNeighbors().size() == 3);
		assertTrue(conn1.getNeighbors().contains(n1));
		assertTrue(conn1.getNeighbors().contains(n2));
		assertTrue(conn1.getNeighbors().contains(n3));

		conn1.removeNeighbor(n2);

		assertTrue(conn1.getNeighbors().size() == 2);
		assertTrue(conn1.getNeighbors().contains(n1));
		assertTrue(conn1.getNeighbors().contains(n3));

		conn1.removeNeighbor(n1);

		assertTrue(conn1.getNeighbors().size() == 1);
		assertTrue(conn1.getNeighbors().contains(n3));

		conn1.removeNeighbor(n3);
		conn1.removeNeighbor(n3);

		assertTrue(conn1.getNeighbors().size() == 0);

	}

	@Test
	public void testFold() {

		Node n1 = new Node(1);
		Node n2 = new Node(2);

		Connections conn1 = new Connections(n1.getConnections(), n1);

		conn1.setFold(n2);

		assertTrue(conn1.getFold().compareTo(n2) == 0);

	}

	@Test
	public void testISF() {

		Node n1 = new Node(1);
		Node n2 = new Node(2);

		Connections conn1 = new Connections(n1.getConnections(), n1);

		conn1.setInverseSurrogateFold(n2);

		assertTrue(conn1.getInverseSurrogateFold().compareTo(n2) == 0);

	}

	@Test
	public void testSurrogateFold() {

		Node n1 = new Node(1);
		Node n2 = new Node(2);

		Connections conn1 = new Connections(n1.getConnections(), n1);

		conn1.setSurrogateFold(n2);

		assertTrue(conn1.getSurrogateFold().compareTo(n2) == 0);

	}

	@Test
	public void testLargestISN() {

		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);

		Connections conn1 = new Connections(n1.getConnections(), n1);

		conn1.addDownPointer(n2);
		conn1.addDownPointer(n3);
		conn1.addDownPointer(n4);

		//assertTrue(conn1.getLargestISN().compareTo(n4) == 0);

	}

}
